﻿using D.Medicalcare.Domain.Entities;
using D.Medicalcare.ErrorCode;
using D.Medicalcare.Infrastructure.Interfaces;
using D.Medicalcare.Read.API.Application.Command.RegistrationCommand;
using D.Medicalcare.Read.API.Dtos;
using MediatR;

namespace D.Medicalcare.Read.API.Application.Handler.RegistrationHandler
{
    /// <summary>
    /// 根据就诊人Id获取就诊人信息
    /// </summary>
    public class GetNoRegistrationHandler : IRequestHandler<GetNoRegistrationCommand, APIResult<GetNoRegistrationDto>>
    {
        private readonly IBaseRepository<Registration> repository;
        private readonly IBaseRepository<Doctor> doctorRepository;
        private readonly IBaseRepository<TitleManagement> titleManagementRepository;
        private readonly IBaseRepository<Department> departmentRepository;
        private readonly IBaseRepository<Patient> patientRepository;
        private readonly IBaseRepository<HospitalNavigation> hospitalNavigationRepository;
        private readonly IBaseRepository<DoorSpend> doorspendRepository;

        /// <summary>
        /// 就诊信息
        /// </summary>
        /// <param name="repository"></param>
        /// <param name="doctorRepository"></param>
        /// <param name="titleManagementRepository"></param>
        /// <param name="departmentRepository"></param>
        /// <param name="patientRepository"></param>
        /// <param name="hospitalNavigationRepository"></param>
        /// <param name="doorspendRepository"></param>
        public GetNoRegistrationHandler(IBaseRepository<Registration> repository, IBaseRepository<Doctor> doctorRepository, IBaseRepository<TitleManagement> titleManagementRepository, IBaseRepository<Department> departmentRepository, IBaseRepository<Patient> patientRepository, IBaseRepository<HospitalNavigation> hospitalNavigationRepository, IBaseRepository<DoorSpend> doorspendRepository)
        {
            this.repository = repository;
            this.doctorRepository = doctorRepository;
            this.titleManagementRepository = titleManagementRepository;
            this.departmentRepository = departmentRepository;
            this.patientRepository = patientRepository;
            this.hospitalNavigationRepository = hospitalNavigationRepository;
            this.doorspendRepository = doorspendRepository;
        }
        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>

        public Task<APIResult<GetNoRegistrationDto>> Handle(GetNoRegistrationCommand request, CancellationToken cancellationToken)
        {
            try
            {
                APIResult<GetNoRegistrationDto> result = new APIResult<GetNoRegistrationDto>();

                var m = from a in repository.GetAll()
                        join b in patientRepository.GetAll() on a.PatientId equals b.PatientId
                        join c in doctorRepository.GetAll() on a.DoctorId equals c.DoctorId
                        join d in titleManagementRepository.GetAll() on c.TitlemanagementId equals d.TitleManagementId
                        join e in departmentRepository.GetAll() on c.DeparId equals e.DeparId
                        join f in hospitalNavigationRepository.GetAll() on e.CampusId equals f.HospitalNavigationId
                        join g  in doorspendRepository.GetAll() on b.PatientNo  equals g.PatientNo
                        select new GetNoRegistrationDto
                        {
                           PatientId=a.PatientId,
                           PatientNo=b.PatientNo,
                           DoctorId=a.DoctorId,
                           DoctorName=c.DoctorName,
                           TitlemanagementId=d.TitleManagementId,
                           JobName=d.JobName,
                           DeparId=e.DeparId,
                           DeparName=e.DeparName,
                           CampusId=f.HospitalNavigationId,
                           CampusName=f.CampusName,
                           BusinessType=g.BusinessType,
                           MoneyType=g.MoneyType

                        };
                result.Code = APIEnum.Success;
                result.Msg = "查询成功";
                result.Data = m.FirstOrDefault(x => x.PatientId == request.PatientId);
                return Task.FromResult(result);



            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
